package com.heihx.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heihx.constant.StatusConstant;
import com.heihx.entity.Dish;
import com.heihx.entity.Orders;
import com.heihx.entity.Setmeal;
import com.heihx.entity.User;
import com.heihx.service.*;
import com.heihx.vo.BusinessDataVO;
import com.heihx.vo.DishOverViewVO;
import com.heihx.vo.OrderOverViewVO;
import com.heihx.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetMealService setMealService;

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        //总订单数
        Long totalOrderCount = orderService.lambdaQuery()
                .between(Orders::getOrderTime, begin, end)
                .count();

        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.select("IFNULL(sum(amount),0) as total");
        wrapper.between("order_time", begin, end);
        wrapper.eq("status", Orders.COMPLETED);
        Map<String, Object> map = orderService.getMap(wrapper);
        BigDecimal turnover = (BigDecimal)map.get("total");

        BigDecimal validOrderCount = BigDecimal.valueOf(orderService.lambdaQuery()
                .between(Orders::getOrderTime, begin, end)
                .eq(Orders::getStatus, Orders.COMPLETED)
                .count());

        BigDecimal unitPrice = BigDecimal.valueOf(0.0);

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount.intValue() != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover.divide(validOrderCount,2, RoundingMode.HALF_UP);
        }

        //新增用户数
        Long newUsers = userService.lambdaQuery()
                .between(User::getCreateTime, begin, end)
                .count();

        return BusinessDataVO.builder()
                .turnover(turnover.doubleValue())
                .validOrderCount(validOrderCount.intValue())
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice.doubleValue())
                .newUsers(newUsers.intValue())
                .build();
    }

    @Override
    public OrderOverViewVO getOrderOverView() {
        //待接单
        Integer waitingOrders = Math.toIntExact(orderService.lambdaQuery()
                .eq(Orders::getStatus, Orders.TO_BE_CONFIRMED)
                .count());

        //待派送
        Integer deliveredOrders = Math.toIntExact(orderService.lambdaQuery()
                .eq(Orders::getStatus, Orders.CONFIRMED)
                .count());

        //已完成
        Integer completedOrders = Math.toIntExact(orderService.lambdaQuery()
                .eq(Orders::getStatus, Orders.COMPLETED)
                .count());

        //已取消
        Integer cancelledOrders = Math.toIntExact(orderService.lambdaQuery()
                .eq(Orders::getStatus, Orders.CANCELLED)
                .count());

        //全部订单
        Integer allOrders = Math.toIntExact(orderService.count());

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    @Override
    public DishOverViewVO getDishOverView() {
        Integer sold = Math.toIntExact(dishService.lambdaQuery()
                .eq(Dish::getStatus, StatusConstant.ENABLE)
                .count());

        Integer discontinued = Math.toIntExact(dishService.lambdaQuery()
                .eq(Dish::getStatus, StatusConstant.DISABLE)
                .count());

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Integer sold = Math.toIntExact(setMealService.lambdaQuery()
                .eq(Setmeal::getStatus, StatusConstant.ENABLE)
                .count());

        Integer discontinued = Math.toIntExact(setMealService.lambdaQuery()
                .eq(Setmeal::getStatus, StatusConstant.DISABLE)
                .count());

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
